<template>
    <div>
        <h3>当前求和的值为:{{sum}}</h3>
        <h3>当前求和放大10倍的值为:{{bigSum}}</h3>
        <h3>学校是:{{school}},地址是:{{addr}}</h3>

        <select v-model.number="n">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
        </select>
        <button @click="increment">+</button>
        <button @click="decrement">-</button>
        <button @click="incrementOdd">当前求和为奇数再加</button>
        <button @click="incrementWait">等一等再加</button>
    </div>
</template>

<script>
    import {mapState,mapGetters} from 'vuex'

    export default {
        name:'Count',
       data() {
           return {
               n:1
           }
       },
       computed:{
           /* sum(){
                return this.$store.state.sum
           },
            bigSum(){
                return this.$store.getters.bigSum
           },
            school(){
                return this.$store.state.school
           },
            addr(){
                return this.$store.state.addr
           }, */
           // 借助 mapstate 生成计算属性
            //...mapState({sum:'sum',school:'school',addr:'addr'}),
            ...mapState(['sum','school','addr']),
            ...mapGetters(['bigSum'])
       },

       methods: {
           increment(){
                //this.$store.dispatch('add', this.n);
                // 直接调用commit
                this.$store.commit('ADD', this.n);
           },
           decrement(){
               //this.$store.dispatch('decrement', this.n);
               this.$store.commit('DECREMENT', this.n);
           },
           // 奇数再加
           incrementOdd(){
               /* if(this.$store.state.sum % 2){
                    this.$store.dispatch('add', this.n);
               } */
               this.$store.dispatch('incrementOdd', this.n);
           },
           incrementWait(){
               /* setTimeout(()=>{
                   this.$store.dispatch('add', this.n);
               },1000) */
               this.$store.dispatch('incrementWait', this.n);
           },
       },
       mounted() {
           mapState({sum:'sum',school:'school',addr:'addr'})
       },
    }
</script>

<style>
    button{
        margin-left: 10px;
    }
</style>